package com.lanou3g.code0523.permission;

import com.sun.deploy.net.HttpResponse;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebFilter(filterName = "UserFilter",urlPatterns = "/user/*")
public class UserFilter implements Filter {
    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest request= (HttpServletRequest) req;
        HttpSession session =request.getSession();
        HttpServletResponse  response = (HttpServletResponse) resp;
        User user =(User)session.getAttribute("user");
        if (user!=null){
            //已通过登录
            chain.doFilter(req, resp);
        }else {
            //没登录 不放行,重定向到login.html
           response.sendRedirect("/login.html");
        }

    }

    public void init(FilterConfig config) throws ServletException {

    }

}
